<?php
/*
 * js_utils.php
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
function is_num ( $v )
{
	if ( intval ( $v ) . "" === "$v" ) return true;

	if ( is_string ( $v ) ) return false;
	// print "Is Num (1): $v\n";

	if ( ! preg_match ( '/^[0-9][0-9]*\.?[0-9]*$/', "$v" ) ) return false;
	// print "Is Num (2): $v\n";
        if ( ! is_numeric ( $v ) ) return false;
	// print "Is Num (3): $v\n";

        $n = floatval ( $v );
        $n .= '';

	// print "Is Num n: $n - $v\n";
	
        if ( "$v" === $n ) return true;

	// print "Is Num FAILED: $v";
        return false;
}

/*
function & str2js ( & $str )
{
	if ( $str === false ) $str = "0";

        $str = str_replace ( array ( '\\', "'" ), array ( "\\\\", "\\'" ), $str );
        $str = preg_replace ( '#([\x00-\x1F])#e', '"\x" . sprintf("%02x", ord("\1"))', $str );

	//$a = Array ( "\r", "", "\n", "<br />", "'", "\\'" );

	//for ( $t = 0; $t < sizeof ( $a ); $t ++ )
	//{
		//$from = $a [ $t ++ ];
		//$to   = $a [ $t ];
//
		//$str = str_replace ( $from, $to, $str );
	//}

	return $str;
}

function & array2js ( & $vals, $full = true, $indent = "", $tab_str = '', $debug = false )
{
	$s = '""';
	if ( ! $vals ) return $s;

	$s = '';

	$cont = 0;

	if ( $full ) $s = $indent . "{";
	foreach ( $vals as $key => $val )
	{
		if ( $cont ) $s .= ", ";

		$s .= "{$indent}'$key': ";

		if ( is_bool ( $val ) )
		{
			if ( $val ) $s .= 'true';
			else	    $s .= 'false';
		} elseif     ( is_num ( $val ) ) $s .= $val;
		elseif ( is_array ( $val ) )   $s .= array2js ( $val, true, $indent . $tab_str, $tab_str );
		else   $s .= "'" . str2js ( $val ) . "'";

		$cont ++;
	}

	if ( $full ) $s .= "{$indent}}";

	if ( $debug ) error_log ( "SERIAL: $s" );

	return $s;
}
*/

function & array2js ( & $vals )
{
	require_once ( "JSON.php" );

	$json = new Services_JSON ();
 	$output = $json->encode ( $vals );

	return $output;
}

/*
	Crea una callback per gli iframe nascosti dell'upload
*/
function & js_cback ()
{
	$len = func_num_args ();
	$cback = util_ff ( "frm_callback", "P" );

	$s = '';

	if ( ! $cback ) return $s;

	$args = '';
	$is_first = true;

	for ( $t = 0; $t < $len; $t ++ )
	{
		if ( ! $is_first ) $args .= ", ";
		$is_first = false;

		$arg = func_get_arg ( $t );
		if ( is_num ( $arg ) ) $args .= $arg;
		else $args .= "'" . str_replace ( "'", "\\'", $arg ) . "'";
	}


	$s = "<script type=\"text/javascript\"> parent.$cback ( $args );</script>";

	return $s;
}
?>
